package com.zh.file.controller;

import com.zh.common.bean.Result;
import com.zh.file.entity.SysAppendix;
import com.zh.file.service.UploadFileService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description:
 * @ClassName FileController
 * @date: 2020.12.10 16:33
 * @Author: zhanghang
 */
@RestController
@RequestMapping
public class UploadFileController {

    @Autowired
    private UploadFileService uploadFileService1;

    /**
     * 文件上传
     *
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation("文件上传")
    @ApiImplicitParam(name = "files", value = "多个文件，", paramType = "formData", allowMultiple = true, required = true, dataType = "file")
    public Result<String> uploadFile(@RequestParam(value = "files", required = true) MultipartFile[] files,@RequestParam(value = "fileType", required = true) String fileType,@RequestParam(value = "fileTypeCode", required = true) String fileTypeCode) throws Exception {
        return uploadFileService1.uploadFilesToServer(files,fileType,fileTypeCode);
    }

    /**
     * 文件下载
     *
     * @param response
     * @throws IOException
     */
    @PostMapping("/download")
    @ApiOperation(value = "附件下载", notes = "附件", httpMethod = "POST")
    public void downloadFile(@RequestBody SysAppendix appendix, HttpServletResponse response) throws IOException {
        uploadFileService1.downloadFile(appendix.getFileName(), appendix.getPath(), response);
    }

    /**
     * 删除单个文件
     *
     * @param id
     * @throws Exception
     */
    @GetMapping(value = "/appendix/{id}")
    @ApiOperation(value = "删除单个文件")
    @ApiImplicitParam(name = "id", value = "主键", required = true, paramType = "path", dataType = "String")
    public void delfile(@PathVariable String id) throws Exception {
        uploadFileService1.deleteFile(Long.valueOf(id));
    }
}
